package com.chinasoft.news.service.impl;

import com.chinasoft.news.customexception.BussinessException;
import com.chinasoft.news.mapper.ArticleCateMapper;
import com.chinasoft.news.pojo.ArticleCate;
import com.chinasoft.news.pojo.Code;
import com.chinasoft.news.pojo.ExType;
import com.chinasoft.news.pojo.Result;
import com.chinasoft.news.service.ArticleCateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ArticleCateImpl implements ArticleCateService {

    ArticleCateMapper articleCateMapper;

    @Override
    public List<ArticleCate> getArticleCate() {
        List<ArticleCate> articleCates = articleCateMapper.selectArticleCateAll();
        return articleCates;
    }

    @Override
    public Boolean insertCate(ArticleCate articleCate) {
        String name = articleCate.getName();
        ArticleCate articleCate1 = articleCateMapper.selectCateByName(name);
        if(articleCate1!=null){
            throw  new BussinessException("insert is failed", ExType.BussCateSame);
        }
        int cate = articleCateMapper.insertArticleCate(articleCate);
        if(cate!=1){
            throw  new BussinessException("insert is failed", ExType.BussInsertCateFail);
        }
        return true;
    }

    @Override
    public ArticleCate getCateByName(String name) {
        ArticleCate articleCate = articleCateMapper.selectCateByName(name);
        return articleCate;
    }

    @Override
    public Boolean deleteCate(Integer id) {
        int i = articleCateMapper.deleteCateById(id);
        if(i!=1){
            throw new BussinessException("delete is fail",ExType.BussDeleteCateFail);
        }
        return  true;
    }

    @Override
    public ArticleCate getCateById(Integer id) {
        ArticleCate articleCate = articleCateMapper.selectCateById(id);
        if(articleCate==null){
            throw new BussinessException("id不存在"+id,ExType.BussInvalidId);
        }
        return articleCate;


    }

    @Override
    public Boolean updateCateById(String name, String alias, Integer id) {
        ArticleCate articleCate = articleCateMapper.selectCateByName(name);
        if(articleCate!=null){
            throw new BussinessException("更新的文章名重复"+id,ExType.BussCateSame);
        }
        int i = articleCateMapper.updateArticleCate(name, alias, id);
        if(i!=1){
            throw new BussinessException("id更新失败"+id,ExType.BussUpdateCateFail);
        }
        return true;
    }
}
